<?php
/**
 * This class is intended to make People request's to the container. People request's are OSViewerFriends or OSOwnerFriends, which allows to get viewer friends and owner friends respectively.
 * Basically to use this class you need instantiate a class that implements the interface OSFriends:
 * <code> $this->viewer_friends = new OSViewerFriends('viewer_friends');
 * //or
 * $this->owner_friends = new OSOwnerFriends('owner_friends');
 * </code>
 * Then you need to create an OSPeopleRequest instance, passing has argument one of the variables previously created:
 * <code>
 * $this->viewer_friends_request = new OSPeopleRequest($this->viewer_friends);
 * //or
 * $this->owner_friends_request = new OSPeopleRequest($this->owner_friends);
 * </code>
 * Now that you have created the OSPeopleRequest object you need to create an OSDataRequest and add that created object to it:
 * <code>
 * $data_request = new OSDataRequest('requestPeople');
 * $data_request->addRequest($this->viewer_friends_request);
 * //or
 * $data_request->addRequest($this->owner_friends_request);
 * </code>
 * After adding the OSPeopleRequest to the OSDataRequest you need to call the function "send()" so that this OSDataRequest will be made to the container:
 * <code>
 * $this->data_response = $data_request->send(true);
 * </code>
 * It has been created the request to the container and now you can access to OSViewerFriends or OSOwnerFriends from the view and iterate over those objects in the view:
 * <code>
 * // View
 * function <? echo $data_response->getJSFunction()->getName() ?>(<? echo $data_response->getJSFunction()->getArgs() ?>) 
 * {
 *   <?php echo $data_response->getData($people_request); ?>
 *  
 *   <?php $person = new OSFriend('person'); ?>
 *   <?php echo $viewer_friends->getVarName() ?>.each(function(<?php echo $person->getVarName() ?>) {
 *     var thumb = <?php echo $person->getField(OSPersonField::THUMBNAIL_URL); ?>
 *     var profile = <?php echo $person->getField(OSPersonField::PROFILE_URL); ?>
 *     html += '<a href="' + profile + '" target="_top" style="float:left">' +'<img src="' + thumb + '"/></a>'; 
 *     document.getElementById('viewer_friends').innerHTML = html;
 *   });
 * }</code>
 * 
 * @package    opensocial
 * @subpackage datarequest
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class OSPeopleRequest implements OSDataRequestable
{ 
  // {1} - Friends Type (VIEWER_FRIENDS/OWNER_FRIENDS)
  // {2} - Response variable for this request( 
  private static $NEWFETCHPEOPLEREQUEST_PATTERN = "newFetchPeopleRequest(%s,%s),\"%s\"";

  // {1} - nome da variavel esta a guardar os parametros
  // {2} - PeopleRequestFields
  // {3} - valor a definir
  private static $PEOPLEREQUESTPARAMS_PATTERN = "%s[%s] = %s;\n";

  //corresponde a um objecto: OSFriends ou OSPerson 
  private $people;

  // Nome da variável usada para os parametros deste PeopleRequest
  private $peopleRequestParams_var;

  // Um array com os parametros
  // exemplo: opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
  private $peopleRequestParams = array();

  private $profileDetails_var;
  private /*OSPersonField[]*/ $profileDetails = array();


 /**
  * OSPeopleRequest constructor
  * @param $people The OSFriends or OSPerson object
  */
  function __construct($people)
  {
    $this->people = $people;
    $this->peopleRequestParams_var = $people->getVarName().'_peopleRequestParams';
    $this->profileDetails_var =  $people->getVarName().'_profileDetailsRequestParams';
  }
  
  public function generateDataRequest(OSDataRequest $dataRequest)
  {
  	//vai buscar o nome da variável que é usada para o dataRequest
    $request_var = $dataRequest->getDataRequestVarName();

    // vai buscar a informação referente ao profile
    $html = 'var '.$this->getProfileDetailsVarName()." = new Array();\n";

    foreach($this->getProfileDetails() as $profileDetail) {
      $html .= $this->getProfileDetailsVarName().".push($profileDetail);\n";
    }

    $params_var = $this->getPeopleRequestParamsVarName();
    $params = $this->getPeopleRequestParams();

    $html .= 'var '.$params_var." = { };\n";

    foreach($params as $key => $value) {
      $html .= sprintf(OSPeopleRequest::$PEOPLEREQUESTPARAMS_PATTERN,$params_var,$key,$value);
    }
    $html .= $request_var.".add(".$request_var.".".sprintf(OSPeopleRequest::$NEWFETCHPEOPLEREQUEST_PATTERN,$this->getPeople()->getType(),$params_var,$this->getPeople()->getVarName()).");\n";
  	return $html;
  }
  
  public function getOptionalKey()
  {
  	return $this->getPeople()->getVarName();
  }
  
  /**
   * This method returns the people object
   * @return OSFriends|OSPerson Returns the people object
   */
  public function getPeople()
  {
    return $this->people;
  }
  
  /**
   * This method returns the var name for profileDetails
   * @return string Profile details var name.
   */
  public function getProfileDetailsVarName()
  {
    return $this->profileDetails_var;
  }
  /**
   * Gets the all the params requested
   * @return array The Request params.
   */
  public function getPeopleRequestParams()
  {
    return $this->peopleRequestParams;
  }
  
  /**
   * Gets the profile details
   * @return array The profile details.
   */
  public function getProfileDetails()
  {
    return $this->profileDetails;
  }
  
  /**
   * Gets the people request variable name
   * @return string The people request variable name.
   */
  public function getPeopleRequestParamsVarName() { return $this->peopleRequestParams_var; }

  /**
   * Set's the sort order. Look at OSDataRequestSortOrder to see the available params.
   * @see OSDataRequestSortOrder
   */
  public function setSortOrder(/*OSDataRequestSortOrder*/ $sortOrder)
  {
    $this->peopleRequestParams[OSDataRequestPeopleRequestFields::SORT_ORDER] = "$sortOrder";
  }

  /**
   * Set's the filter type. Look at OSDataRequestFilterType to see the available params.
   * @see OSDataRequestFilterType
   */
  public function setFilterType(/*OSDataRequestFilterType*/ $filterType)
  {
    $this->peopleRequestParams[OSDataRequestPeopleRequestFields::FILTER] = "$filterType";
  }

  /**
   * Set's the index of the first item to fetch.
   * @param int $first The index of the first item to fetch.
   * @link http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.PeopleRequestFields.html#FIRST
   */
  public function setFirst(/*Number*/ $first)
  {
    $this->peopleRequestParams[OSDataRequestPeopleRequestFields::FIRST] = "$first";
  }
  
  /**
   * Set's the maximum number of items to fetch; 
   * defaults to 20. If set to a larger number, a container may honor the request, or may limit the number to a container-specified limit of at least 20
   * 
   * @param int $max The maximum number of items to fetch.
   * @link http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.PeopleRequestFields.html#MAX
   */
  public function setMax(/*Number*/ $max)
  {
    $this->peopleRequestParams[OSDataRequestPeopleRequestFields::MAX] = "$max";
  }
  
  /**
   * Set's the profile details array.
   * @param array One array containing the profile details.   
   */
  public function setProfileDetails(/*OSPersonField*/ $profileDetails)
  {
    if(count($profileDetails))
      $this->peopleRequestParams[OSDataRequestPeopleRequestFields::PROFILE_DETAILS] = $this->profileDetails_var;
    $this->profileDetails = $profileDetails;
  }

  /**
   * Add's an request for profile detail. Look at OSPersonField to see the available params.
   * @see OSPersonField
   */
  public function addProfileDetails(/*OSPersonField*/ $profileDetail)
  {
    if(count($this->profileDetails) == 0 )
      $this->peopleRequestParams[OSDataRequestPeopleRequestFields::PROFILE_DETAILS] = $this->profileDetails_var;
    $this->profileDetails[] = $profileDetail;
  }
  
}
